< previous page page_17 next page >

Page 17
The LastErr.vbp project (on the CD that comes with this book) has a form with a text box named txtCaption into which you can type the caption of the main window of any program. The FindWindow function retrieves the handle to that window. The program then uses the PostMessage function to post a WM_CLOSE message into the message queue for the windowthis is the same message that is posted when you use the close box or system menu to close a window.
Private Declare Function PostMessage Lib "User32" Alias _
"PostMessageA" (ByVal hWnd As Long, ByVal Message As Long, ByVal _
wParam As Long, ByVal lParam As Long) As Long
Private Const WM CLOSE = &H10


Private Sub cmdClose_Click()
   Dim WindowHandle As Long
   WindowHandle = FindWindow("", txtCaption.Text)
   If WindowHandle = 0 Then
      MsgBox "No window: Last Error was " & Err.LastDllError
      Exit Sub
   Else
      Call PostMessage(WindowHandle, WM_CLOSE, 0, 0)
   End If
End Sub
Results
The default caption is ''Untitled Notepad," which is the caption of a default Notepad application window. The program fails. The message box displays the LastError code, a value provided by the operating system that explains the reason for the error.2 Your mission is twofold:
1. Find the meaning of the last error code.
2. Fix the program so that it will work.
7017a6ead0e3c4111b47a554df321e9f.gif 7017a6ead0e3c4111b47a554df321e9f.gif
2 The LastDllError code will be 123 on Windows NT, 0 on Windows 95/98.

 
< previous page page_17 next page >